package com.xu.demo.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xu.demo.common.R;
import com.xu.demo.entity.Gt;
import com.xu.demo.entity.Post;
import com.xu.demo.service.GtService;
import com.xu.demo.service.PostService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import java.io.File;
import java.io.IOException;
import java.util.*;

@CrossOrigin
@RestController
@RequestMapping("/post")
public class PostController {
    @Autowired
    PostService postService;
    @Autowired
    GtService gtService;
    @Value("${share1.datapath}")
    private  String fileUploadPath;
    @Value("${share1.path}")
    private  String basePath;
    @PostMapping(value = "/taddpost")
    public String upload( @RequestParam Map<String, Object> params,@RequestParam("file") MultipartFile[] files) throws IOException {
        System.out.println(params);
        Post post = new Post();
        if (files.length == 1) {
            post.setGs(1L);
            String name = files[0].getOriginalFilename();
            String fileName = UUID.randomUUID().toString() + name;//dfsdfdfd.jpg]
            files[0].transferTo(new File(fileUploadPath + fileName));
            post.setZ1(name);
            post.setZl1(fileName);
        }
        else if (files.length == 2) {
            post.setGs(2L);
            String name0 = files[0].getOriginalFilename();
            String fileName0 = UUID.randomUUID().toString() + name0;
            files[0].transferTo(new File(fileUploadPath + fileName0));
            String name1 = files[1].getOriginalFilename();
            String fileName1 = UUID.randomUUID().toString() + name1;
            files[1].transferTo(new File(fileUploadPath + fileName1));
            post.setZ1(name0);
            post.setZl1(fileName0);
            post.setZ2(name1);
            post.setZl2(fileName1);
        }

        post.setName(params.get("postName").toString());
        post.setContent(params.get("content").toString());
        System.out.println(post.toString());
        postService.save(post);
        return "A";
    }
    @PostMapping("/addpost")
    public R<String> upplioa(@RequestBody Post post){
        System.out.println(post.toString());

        postService.saveOrUpdate(post);
        return R.success("保存成功");
    }

    @PostMapping("/imgUpload")
    public Map<String,Object> uploadImg(MultipartFile file) throws IOException {
        String path = basePath;
        Map<String,Object> result=new HashMap<>();
        String name =file.getOriginalFilename();
        //使用UUID重新生成文件名，防止文件名称重复造成文件覆盖
        String fileName = UUID.randomUUID().toString() + name;//dfsdfdfd.jpg
        file.transferTo(new File(path+fileName));
        System.out.println(fileName);
        // 这里我使用了 Nginx 进行反向代理访问图片，可根据个人需要进行改动
        List<Map<String,Object>> datas=new ArrayList<Map<String, Object>>();
        Map<String,Object> data=new HashMap<>();
        data.put("url","http://localhost:8082/imgUploads/"+fileName);
        datas.add(data);
        result.put("errno",0);
        result.put("data",datas);
        return result;
    }
    @GetMapping("/test")
    public R<Page> page(int pageNum, int pageSize,String name) {
        Page pageInfo=new Page(pageNum,pageSize);
//        构造条件构造器
        LambdaQueryWrapper<Post> queryWrapper=new LambdaQueryWrapper();
        queryWrapper.like(name != null, Post::getName, name);

        queryWrapper.orderByDesc(Post::getFlag);
        queryWrapper.orderByDesc(Post::getNewTime);

        //执行查询
        postService.page(pageInfo,queryWrapper);
        return R.success(pageInfo);
    }
    @GetMapping("/{tz_id}")
    public R<Post> detail(@PathVariable Integer tz_id){
        Post p=postService.getById(tz_id);
        return R.success(p);
    }
//    删除帖子 并且删除这个帖子下面所有的跟帖
    @DeleteMapping("/{tz_id}")
    public R<String> deletePost(@PathVariable Integer tz_id){
        postService.removeById(tz_id);
        LambdaQueryWrapper<Gt> queryWrappeer=new LambdaQueryWrapper<>();
        queryWrappeer.eq(Gt::getTz_id,tz_id);
        gtService.remove(queryWrappeer);
        return R.success("删除成功");
    }
    @PostMapping("/top/{tz_id}")
    public R<String> top(@PathVariable Integer tz_id){
        Post p=postService.getById(tz_id);
        p.setFlag(1l);
        postService.saveOrUpdate(p);
        return R.success("置顶成功");
    }
    @PostMapping("/bottom/{tz_id}")
    public R<String> bottom(@PathVariable Integer tz_id){
        Post p=postService.getById(tz_id);
        p.setFlag(0l);
        postService.saveOrUpdate(p);
        return R.success("取消置顶成功");
    }


}
